local sk__taoluan = fk.CreateSkill {

  name = "sk__taoluan",

  tags = {  },

}



sk__taoluan:addEffect(fk.TargetSpecified, {
  name = "sk__taoluan",
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__taoluan.name) and (data.card.type == Card.TypeBasic or data.card:isCommonTrick()) and data.firstTarget and
      table.every(player.room.alive_players, function(p)
        return not p.dying
      end) and not player:isNude() then
      local all_choices = {}
      if data.card.type == Card.TypeBasic then
        all_choices = Fk:getAllCardNames("b")
      elseif data.card:isCommonTrick() then
        all_choices = Fk:getAllCardNames("t")
      end
      local choices = {}
      for _, name in ipairs(all_choices) do
        local card = Fk:cloneCard(name)
        if not table.contains(player:getTableMark("sk__taoluan-round"), card.trueName) and
          name ~= data.card.name and
          target:canUse(card, {bypass_distances = true, bypass_times = true}) and
          (not card.skill:getMinTargetNum(player) or card.skill:getMinTargetNum(player) < 2) then
          table.insert(choices, name)
        end
      end
      if #choices > 0 then
        event:setCostData(self, {choices, all_choices})
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "sk__taoluan-tmp", event:getCostData(self))
    local success, dat = room:askForUseActiveSkill(player, "sk__taoluan_active",
      "#sk__taoluan-invoke::"..target.id..":"..data.card:toLogString(), true)
    room:setPlayerMark(player, "sk__taoluan-tmp", 0)
    if success then
      event:setCostData(self, dat)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local name = event:getCostData(self).interaction
    local mark = player:getTableMark("sk__taoluan-round")
    table.insert(mark, Fk:cloneCard(name).trueName)
    room:setPlayerMark(player, "sk__taoluan-round", mark)
    room:throwCard(event:getCostData(self).cards, sk__taoluan.name, player, player)
    room:sendLog{
      type = "#SkChangeCardSkill",
      from = player.id,
      to = { target.id },
      arg = sk__taoluan.name,
      arg2 = data.card:toLogString(),
      arg3 = name,
    }
    local use = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
    if use ~= nil then
      local new_card = Fk:cloneCard(name, data.card.suit, data.card.number)
      local c = table.simpleClone(data.card)
      for k, v in pairs(c) do
        if new_card[k] == nil then
          new_card[k] = v
        end
      end
      if data.card:isVirtual() then
        new_card.subcards = data.card.subcards
      else
        new_card.id = data.card.id
      end
      new_card.skillNames = data.card.skillNames
      new_card.skill = Fk:cloneCard(name).skill
      data.card = new_card
      use.data.card = new_card
    end
  end,
})

return sk__taoluan